using System;
using Company.Infrastructure;
using Company.Model;
using Company.Model.Services;

namespace Company.Services
{
    public class SessionService : ISessionService
    {
        private IFormsAuthenticationService m_FormsAuthenticationService = new FormsAuthenticationService();

        public void SigIn(IUsers User)
        {
            ISessionData m_Data = BuilUserData(User);
            string m_CokkieData = CustomSerialize.SerializarToXml(m_Data);
            m_FormsAuthenticationService.SignIn(m_Data.CockieName,false,m_CokkieData);
        }

        private ISessionData BuilUserData(IUsers User)
        {
            ISessionData Data = new SessionData();
            Data.CockieName = User.Id.ToString().Trim();
            Data.Color = "red";
            Data.UserName = User.UserName.Trim();
            Data.CompanyID = User.EmpresaModel.IdEmpresa.ToString().Trim();
            Data.CompanyName = User.EmpresaModel.Nombre.Trim();
            Data.IDPersona = User.IdPersona.ToString().Trim();
            return Data;
        }

        public ISessionData GetCurrentSessionData()
        {
            string userData = m_FormsAuthenticationService.GetUserData();
            ISessionData m_Data = new SessionData();
            m_Data = CustomSerialize.DeserializarTo<SessionData>(userData);
            return m_Data;
        }

        public void SignOut()
        {
            m_FormsAuthenticationService.SignOut();
        }
    }
}